package com.sinocare.project.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.project.common.domain.SystemAccount;
import com.sinocare.project.common.dopackage.SystemDo;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.common.vo.SystemVo;
import com.sinocare.project.system.service.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/system")
@Api(tags = "系统账号模块")
public class SystemController {

    @Resource
    private SystemService systemServiceImpl;

    @RequestMapping(value = "all",method = RequestMethod.GET)
    @ApiOperation(value = "查询信息",notes = "查询账号信息")
    public Result all(Integer page , Integer limit, String searchParams) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();

        SystemVo systemVo = null;


        if(searchParams != null){
            systemVo = objectMapper.readValue(searchParams,SystemVo.class);
        }



        Page pages = PageHelper.startPage(page, limit);
        List<SystemDo> users = systemServiceImpl.query(systemVo);

        return Result.ok(ResultCode.SUCCESS,pages.getTotal(),users);
    }


    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ApiOperation(value = "修改",notes = "修改账号信息")
    public Result updatesystem(SystemDo systemDo) throws Exception{



        return Result.ok(ResultCode.SUCCESS,systemServiceImpl.update(systemDo));

    }


    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "添加",notes = "添加账号信息")
    public Result addsystem(SystemAccount systemAccount) throws Exception{

        int add = systemServiceImpl.add(systemAccount);
        if (add>0){
            systemServiceImpl.addstore(systemAccount);
            systemServiceImpl.addrole(systemAccount);


        }

        return Result.ok(ResultCode.SUCCESS,0);
    }







}
